﻿using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using UnityEngine;
using Utils;
using Utils.ModuleCoroutine;
using Utils.ModuleHint;



namespace Server_Management.Event
{

    /// <summary>
    /// 
    /// </summary>
    public class HintBroadCast
    {
        [PluginEvent(ServerEventType.PlayerJoined)]
        public void OnPlayerJohn(PlayerJoinedEvent ev)
        {
            TimeCoroutine.Delay(1f, () =>
            {
                ModuleCoroutine module = CoroutineCenter.Get($"{CoroutineType.HintBox}");

                bool isGet = module.TryGet(ev.Player.UserId, out Coroutine output);
                if (isGet)
                {
                    TimeCoroutine.Stop(output);
                }

                Coroutine newCOR = TimeCoroutine.Loop(
                    -1,
                    1,
                    () =>
                    {                       
                        ev.Player.ReceiveHint(HintUtil.GetHint(ev.Player));
                    });

                module.DoRecord(ev.Player.UserId, newCOR);
            });
        }


        [PluginEvent(ServerEventType.PlayerLeft)]
        public void OnPlayerLeft(PlayerLeftEvent ev)
        {
            if (ev.Player == null) return;

            CoroutineCenter.Get($"{CoroutineType.HintBox}").DoDisposed(ev.Player.UserId);
        }

        [PluginEvent(ServerEventType.RoundRestart)]
        public void OnRoundRestart(RoundRestartEvent ev)
        {
            CoroutineCenter.Get($"{CoroutineType.HintBox}").DoDisposed();
        }

        [PluginEvent(ServerEventType.RoundEnd)]
        public void OnRoundEnd(RoundEndEvent ev)
        {
            CoroutineCenter.Get($"{CoroutineType.HintBox}").DoDisposed();
        }
    }
}
